/******************************************************************************
*    Copyright (C), 2022 by D2X studio. All rights reserved.                  *
*                                                                             *
*    @file:      terminal.c
*    @owner:     zhengjian.xie@gmail.com                                      *
*    @desc:                                                                   *
*    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +    *
* history:                                                                    *
*  <author>      <time>      <version>              <description>             *
*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  *
*  |  Xzj |  20220411[23:46] | 1.0.0 |                  build              |  *
*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  *
******************************************************************************/
#include "includes.h"
#include <unistd.h>

/** terminal data define */
static stEvent __terminal_evt__ = { STDIN_FILENO, no, nil };

void terminal_callback(void* pVoid) {
    int ret;
    char buf[128] = { 0 };

    ret = read(STDIN_FILENO, buf, sizeof(buf));
    printf("terminal: %s\n", buf);

    if ( 'q' == buf[0] ) {
        app_exit();
        return ;
    }

    if ( 's' == buf[0] ) {
#if defined(TCU_SERVER)
        sock_server_sendstr("Hello, I'm server!");
#elif defined(TCU_CLIENT)
        sock_server_sendstr("Hello, I'm client!");
#else
    printf("TCU app say goodbye for you!\n");
#endif
    }
}

/** terminal_init */
void terminal_init(void) {
    __terminal_evt__.doEvent = terminal_callback;
    app_event_add(&__terminal_evt__);
}

/** terminal_deinit */
void terminal_deinit(void) {
    app_event_del(&__terminal_evt__);
}
